﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRO_DOCS.DocumentManagementSystem.Petrolium.WIN.Back
{
    public partial class AdjustZippingSettings : BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        public AdjustZippingSettings()
        {
            InitializeComponent();
        }

        public string ZipCommand { get; set; }
        public string UnZipCommand { get; set; }

        private void formatComboBoxEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            compressionMethodComboBoxEdit.ResetText();
            switch (formatComboBoxEdit.Text)
            {
                case "7z":
                    compressionMethodComboBoxEdit.Properties.Items.Clear();
                    compressionMethodComboBoxEdit.Properties.Items.AddRange(new object[]
                                                                                {
                                                                                    "LZMA", "LZMA2", "PPMd", "BZip2"
                                                                                    //, "Deflate", "Delta", "BCJ", "BCJ2", "Copy" //filters
                                                                                });
                    encryptionMethodComboBoxEdit.Properties.Items.Clear();
                    encryptionMethodComboBoxEdit.Properties.Items.AddRange(new object[]
                                                                               {
                                                                                   "AES256"
                                                                               });

                    //splitComboBoxEdit.Properties.Items.Clear(); 
                    //splitComboBoxEdit.Properties.Items.AddRange(new object[]
                    //                                                {
                    //                                                    "10M",
                    //                                                    "650M - CD",
                    //                                                    "700M - CD",
                    //                                                    "4480M - DVD",
                    //                                                    "1457664 - 3.5\" floppy"
                    //                                                });
                    break;
                case "bzip2":
                    encryptionMethodComboBoxEdit.Properties.Items.Clear();
                    compressionMethodComboBoxEdit.Properties.Items.Clear();
                    compressionMethodComboBoxEdit.Properties.Items.AddRange(new object[]
                                                                                {
                                                                                    "BZip2"
                                                                                });
                    break;
                case "gzip":
                    encryptionMethodComboBoxEdit.Properties.Items.Clear();
                    compressionMethodComboBoxEdit.Properties.Items.Clear();
                    compressionMethodComboBoxEdit.Properties.Items.AddRange(new object[]
                                                                                {
                                                                                    "Deflate"
                                                                                });

                    break;
                case "tar":
                    encryptionMethodComboBoxEdit.Properties.Items.Clear();

                    compressionMethodComboBoxEdit.Properties.Items.Clear();

                    break;
                case "wim":
                    encryptionMethodComboBoxEdit.Properties.Items.Clear();
                    compressionMethodComboBoxEdit.Properties.Items.Clear();

                    break;
                case "xz":
                    encryptionMethodComboBoxEdit.Properties.Items.Clear();
                    compressionMethodComboBoxEdit.Properties.Items.Clear();
                    compressionMethodComboBoxEdit.Properties.Items.AddRange(new object[]
                                                                                {
                                                                                    "LZMA2"
                                                                                });

                    break;
                case "zip":

                    compressionMethodComboBoxEdit.Properties.Items.AddRange(new object[]
                                                                                {
                                                                                    //"Copy",
                                                                                    "Deflate",
                                                                                    "Deflate64",
                                                                                    "BZip2",
                                                                                    "LZMA",
                                                                                    "PPMd"
                                                                                });
                    encryptionMethodComboBoxEdit.Properties.Items.Clear();
                    encryptionMethodComboBoxEdit.Properties.Items.AddRange(new object[]
                                                                               {
                                                                                   "ZipCrypto",
                                                                                   //"AES128",
                                                                                   //"AES192",
                                                                                   "AES256"
                                                                               });
                    break;
            }
            RefreshThreadCounts();
        }


        private void RefreshThreadCounts()
        {
            numberOfThreadsComboBoxEdit.ResetText();
            if (compressionMethodComboBoxEdit.Text.ToLower().IndexOf("lzma", StringComparison.OrdinalIgnoreCase) == -1)
            {
                numberOfThreadsComboBoxEdit.Properties.Items.Clear();
                numberOfThreadsComboBoxEdit.ResetText();
                return;
            }

            switch (formatComboBoxEdit.Text)
            {
                case "7z":
                    numberOfThreadsComboBoxEdit.Properties.Items.Clear();
                    numberOfThreadsComboBoxEdit.Properties.Items.AddRange(new object[] { "1", "2" });
                    break;
                case "bzip2":
                    numberOfThreadsComboBoxEdit.Properties.Items.Clear();
                    for (int i = 1; i < Environment.ProcessorCount * 2; i++)
                        numberOfThreadsComboBoxEdit.Properties.Items.Add(i.ToString());
                    numberOfThreadsComboBoxEdit.Text = Environment.ProcessorCount.ToString();
                    break;
                case "gzip":
                    numberOfThreadsComboBoxEdit.Properties.Items.Clear();
                    break;
                case "tar":
                    numberOfThreadsComboBoxEdit.Properties.Items.Clear();
                    break;
                case "wim":
                    numberOfThreadsComboBoxEdit.Properties.Items.Clear();
                    break;
                case "xz":
                    numberOfThreadsComboBoxEdit.Properties.Items.Clear();
                    for (int i = 1; i < Environment.ProcessorCount * 2; i++)
                        numberOfThreadsComboBoxEdit.Properties.Items.Add(i.ToString());
                    numberOfThreadsComboBoxEdit.Text = Environment.ProcessorCount.ToString();
                    break;
                case "zip":
                    numberOfThreadsComboBoxEdit.Properties.Items.Clear();
                    for (int i = 1; i < Environment.ProcessorCount * 2; i++)
                        numberOfThreadsComboBoxEdit.Properties.Items.Add(i.ToString());
                    numberOfThreadsComboBoxEdit.Text = Environment.ProcessorCount.ToString();
                    break;
            }
        }

        private void ZippingSettings_Load(object sender, EventArgs e)
        {
            cpuThreadsLabelControl.Text = "/" + Environment.ProcessorCount;
            compressionMethodComboBoxEdit.Properties.Items.AddRange(new object[]
                                                                        {
                                                                            "LZMA", "LZMA2", "PPMd", "BZip2"
                                                                            //, "Deflate", "Delta", "BCJ", "BCJ2", "Copy" //filters
                                                                        });
            RefreshThreadCounts();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(formatComboBoxEdit.Text))
                ZipCommand = " -t" + formatComboBoxEdit.Text;

            switch (copressionLevelComboBoxEdit.Text)
            {
                case "Store":
                    ZipCommand += " -mx0";
                    break;
                case "Fastest":
                    ZipCommand += " -mx1";
                    break;
                case "Fast":
                    ZipCommand += " -mx3";
                    break;
                case "Normal":
                    ZipCommand += " -mx5";
                    break;
                case "Maximum":
                    ZipCommand += " -mx7";
                    break;
                case "Ultra":
                    ZipCommand += " -mx9";
                    break;
            }

            if (!string.IsNullOrEmpty(compressionMethodComboBoxEdit.Text) && formatComboBoxEdit.Text == "zip")
                ZipCommand += " -mm=" + compressionMethodComboBoxEdit.Text;
            else
                ZipCommand += " -m0=" + compressionMethodComboBoxEdit.Text;

            if (!string.IsNullOrEmpty(encryptionMethodComboBoxEdit.Text) && formatComboBoxEdit.Text == "zip")
                ZipCommand += " -mem=" + encryptionMethodComboBoxEdit.Text;

            if (!string.IsNullOrEmpty(numberOfThreadsComboBoxEdit.Text))
                ZipCommand += " mt=" + numberOfThreadsComboBoxEdit.Text;


            if (splitComboBoxEdit.Text == "1457664 - 3.5\" floppy")
            {
                ZipCommand += " -v3.5m";
            }
            else if (splitComboBoxEdit.Text != "")
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(splitComboBoxEdit.Text,
                                                                 "^([0-9]+(.[0-9]+)?)[b|B|k|K|m|M|g|G]$"))
                {
                    ZipCommand += " -v" + splitComboBoxEdit.Text;
                }
                else
                {
                    XtraMessageBox.Show("Please enter valid value fot the split");
                    return;
                }
            }

            if (passTextEdit.Text != "")
            {
                if (passTextEdit.Text == rePassTextEdit.Text)
                {
                    if (formatComboBoxEdit.Text == "7z")
                        ZipCommand += " -mhe -p" + passTextEdit.Text;
                    else
                        ZipCommand += " -p" + passTextEdit.Text;

                    UnZipCommand += " -p" + passTextEdit.Text;
                }
                else
                {
                    XtraMessageBox.Show("Password not mathced");
                    return;
                }
            }

            switch (overwriteModeComboBoxEdit.Text)
            {
                case "Overwrite without prompt":
                    UnZipCommand += " -aoa";
                    break;
                case "Skip existing files":
                    UnZipCommand += " -aos";
                    break;
                case "Auto rename":
                    UnZipCommand += " -aou";
                    break;
                case "Auto rename existing files":
                    UnZipCommand += " -aot";
                    break;
            }

            //------------------------->DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
           //--------------------------> DialogResult = DialogResult.Cancel;
        }

        private void compressionMethodComboBoxEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshThreadCounts();
        }
    }
}